const api = require('../../../ws_micro_shop/utils/api.js');

const app = getApp();
Page({
  data: {
    isOpen: false,
    isShow: false,
    levelInfo: {},
    description: '',
    img_url: app.imgUrl,
  },

  onShow() {
    this.setData({
      mobiles: wx.getStorageSync('user_info').mobile,
    })
    this.getDescription();
  },

  change() {
    app.util.navigateTo('/ws_micro_shop/pages/userinfo/userinfo', {}, false);
  },

  getDescription() {
    wx.showLoading({
      title: '正在加载...',
      mask: true,
    });
    try {
      app.ws_micro_shoprequest({
        url: api.recharge.info,
        success: res => {
          if (res.code === 0) {
            let data = res.data || [];
            this.setData({
              levelInfo: data.level,
              policy: data.member_content,
              description: data.help,
              nickname: data.nickname,
              avatar_url: data.avatar_url,
            });
          } else {
            wx.showToast({
              title: res.msg || '',
              icon: 'none',
            });
          }        
        },
        complete: () => {
          wx.hideLoading();
        }
      });
    } catch (error) {
      console.log('openCard error', error);
    }
  },
  bindphone(e) {
    let that = this;
    app.ws_micro_shoplogin(1).then(res => {
      if (e.detail.errMsg === 'getPhoneNumber:ok') {
        app.ws_micro_shoprequest({
          url: api.passport.bind_mobile,
          data: {
            encrypted_data: e.detail.encryptedData,
            iv: e.detail.iv,
          },
          method: 'POST',
          success: function(res) {
            if (res.code === 0) {
              wx.showToast({
                title: '手机号绑定成功',
                icon: 'success',
              });
              that.setData({
                mobile: res.data.mobile,
              });
              that.loadData();
            } else {
              wx.showModal({
                title: '提示',
                content: '绑定失败，请重新登录',
                success: function(res) {
                  if (res.confirm) {
                    that.bindphone(e);
                  }
                },
              });
            }
          },
        });
      } else {
        wx.showToast({
          title: '您已取消绑定该操作',
          icon: 'none',
        });
      }
    });
  },
});
